<?php

require_once("lib/alipay_submit.class.php");

class AliPay {

    //公用参数
    public $notify_url   = '';//服务器异步通知页面路径  需http://格式的完整路径，不允许加?id=123这类自定义参数
    //付款请求参数
    public $return_url   = '';//页面跳转同步通知页面路径
    public $orderid      = '';//必填，订单id
    public $subject      = '';//必填商品名称
    public $price        = '';//必填，价格
    public $message      = '';//订单描述
    public $show_url     = '';//商品展示地址
    public $anti_key     = '';//防钓鱼时间戳，若要使用请调用类文件submit中的query_timestamp函数
    public $exter_ip     = '';//客户端IP地址，非局域网的外网IP地址，如：221.0.0.1
    public $is_b_pay     = '1h';//选填，超时时间  ,默认一个小时
    public $isMobile     = FALSE;//是否是移动设备
    //退款请求参数
    public $seller_email = "";//必填，卖家支付宝帐户
    public $refund_date  = "";//必填，退款当天日期，格式：年[4位]-月[2位]-日[2位] 小时[2位 24小时制]:分[2位]:秒[2位]，如：2007-10-01 13:13:13
    public $batch_no     = "";//必填，批次号，格式：当天日期[8位]+序列号[3至24位]，如：201008010000001
    public $batch_num    = "";//必填，退款笔数，参数detail_data的值中，“#”字符出现的数量加1，最大支持1000笔（即“#”字符出现的数量999个）
    public $detail_data  = "";//退款详细数据，必填，具体格式请参见接口技术文档

    //支付
    public function pay($alipay_config = array()) {
        if( !$alipay_config )   $alipay_config = C("ALIPAY_CONFIG_NEW");//载入支付宝配置
        //判断是手机支付还是PC支付 默认PC支付
        $service       = "create_direct_pay_by_user";
        $seller        = "seller_email";
        if($this->isMobile) {
            $service = "alipay.wap.create.direct.pay.by.user";
            $seller  = "seller_id";
        }
    
        $parameter = array(
            "service"            => $service,
            "partner"            => trim($alipay_config['partner']),
            $seller              => trim($alipay_config['seller_email']),
            "payment_type"       => '1',
            "notify_url"         => $this->notify_url,
            "return_url"         => $this->return_url,
            "out_trade_no"       => $this->orderid,
            "subject"            => $this->subject,
            "total_fee"          => $this->price,
            "extra_common_param" => $this->extra_common_param,
            "body"               => $this->message,
            "show_url"           => $this->show_url,
            "it_b_pay"           => $this->is_b_pay,
            "anti_phishing_key"  => $this->anti_key,
            "exter_invoke_ip"    => $this->exter_ip,
            "_input_charset"     => trim(strtolower($alipay_config['input_charset']))
        );
    
        //建立请求
         $alipaySubmit = new AlipaySubmit($alipay_config);
         $html_text    = $alipaySubmit->buildRequestForm($parameter,"get",$this->waitword);
         return $html_text;
        /* $alipaySubmit = new AlipaySubmit($alipay_config);
        $para = $alipaySubmit->buildRequestPara($parameter);
        return $alipaySubmit->alipay_gateway_new.http_build_query($para); */
    }
    
    
    
    //退款
    public function Refund() {
        $alipay_config = C("ALIPAY_CONFIG");//载入支付宝配置
        $parameter     = array(
            "service"        => "refund_fastpay_by_platform_pwd",
            "partner"        => trim($alipay_config['partner']),
            "notify_url"     => $this->notify_url,
            "seller_email"   => trim($alipay_config['seller_email']),
            "refund_date"    => $this->refund_date,
            "batch_no"       => $this->batch_no,
            "batch_num"      => $this->batch_num,
            "detail_data"    => $this->detail_data,
            "_input_charset" => trim(strtolower($alipay_config['input_charset']))
        );

        //建立请求
        $alipaySubmit = new AlipaySubmit($alipay_config);
        $html_text    = $alipaySubmit->buildRequestForm($parameter,"get","确认");
        echo $html_text;
    }

    //生成移动app请求数据
    public function AppAlipayInfo() {
        $alipay_config = C("ALIPAY_CONFIG");//载入支付宝配置
        $parameter     = array(
            "service"        => "mobile.securitypay.pay",
            "partner"        => trim($alipay_config['partner']),
            "_input_charset" => trim(strtolower($alipay_config['input_charset'])),
            "notify_url"     => $this->notify_url,
            "app_id"         => "",
            "appenv"         => "",
            "out_trade_no"   => $this->orderid,
            "subject"        => $this->subject,
            "payment_type"   => '1',
            "seller_id"      => trim($alipay_config['seller_email']),
            "total_fee"      => $this->price,
            "body"           => $this->message,
            "it_b_pay"       => $this->is_b_pay,
        );

        //建立请求
        $alipaySubmit = new AlipaySubmit($alipay_config);
        $strAlipayInfo = $alipaySubmit->buildRequestParaToString($parameter);
        return $strAlipayInfo;
    }

}
